package com.terry.gamehall.test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author tanlvxu
 * @version 1.0
 * @information
 * @date 2021/10/18 9:49
 */
public class CompletableFutureTest5 {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws Exception {
        //多任务组合
        //1.allof等待所有任务完成
        //2.anyof只要有一个任务完成
        System.out.println("main start!");
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一"+Thread.currentThread().getId()+"开始");
            int i = 10 / 2;
            System.out.println("运行结果 " + i);
            System.out.println("任务一"+Thread.currentThread().getId()+"结束");
            return i+"";
        }, executor);


        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二"+Thread.currentThread().getId()+"开始");
            System.out.println("任务二"+Thread.currentThread().getId()+"结束");
            return "hello";
        }, executor);

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务三"+Thread.currentThread().getId()+"开始");
            System.out.println("任务三"+Thread.currentThread().getId()+"结束");
            return "world";
        }, executor);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(future, future2, future3);
//
//        allOf.get();
//        System.out.println("main end!"+future.get()+"--"+future2.get()+"----"+future3.get());

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future, future2, future3);

        System.out.println("main end!"+anyOf.get());
        System.out.println("main end!"+future.get()+"--"+future2.get()+"----"+future3.get());
    }

}
